import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


class Students{
	int id;
	char[] stuno;
	char[] name;
	int age;
	Date birthdate;
}

public class ex2 {
	public static int insert(Students stu) throws ClassNotFoundException{
		
		String URL = "jdbc:mysql://localhost:3306/test";
		String driverName = "com.mysql.jdbc.Driver";
		String sql = "insert into students(stuno,name,gender,birthdate,major,age) values('201010001', 'lisi', 'm', '1992-10-01','cs',25)";
		String userName = "root";//root
		String password = "kfc123456";//123456
		Connection conn = null;
		
		Class.forName(driverName);//jdbc4.0 ������ʹ������������ע�����
		
		
		try {
			conn = DriverManager.getConnection(URL,userName,password);
			Statement statement = conn.createStatement();
			//ResultSet resultSet = statement.executeQuery(sql);
			int resultSet = statement.executeUpdate(sql);
			// id | stuno     | name | gender | birthdate  | major
//			while(resultSet.next()){
//				int id = resultSet.getInt("id");
//				String stuno = resultSet.getString("stuno");
//				Date date = resultSet.getDate("birthdate");
//				System.out.print("id="+id+" stuno="+stuno+" birthdate="+date);
//			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//����ʹ��Try..with..resources�﷨��
			conn = null;
		}
		return 0;
		
	}
	public static void displayAll() throws ClassNotFoundException{
		//String sql = "select stuno,name,brithdate from students";
		String URL = "jdbc:mysql://localhost:3306/test";
		String driverName = "com.mysql.jdbc.Driver";
		String sql = "select * from students";
		String userName = "root";//root
		String password = "kfc123456";//123456
		Connection conn = null;
		
		Class.forName(driverName);//jdbc4.0 ������ʹ������������ע�����
		
		
		try {
			conn = DriverManager.getConnection(URL,userName,password);
			Statement statement = conn.createStatement();
			ResultSet resultSet = statement.executeQuery(sql);
			// id | stuno     | name | gender | birthdate  | major
			while(resultSet.next()){
				int id = resultSet.getInt("id");
				String stuno = resultSet.getString("stuno");
				Date date = resultSet.getDate("birthdate");
				System.out.print("id="+id+" stuno="+stuno+" birthdate="+date);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if(conn!=null)
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}//����ʹ��Try..with..resources�﷨��
			conn = null;
		}
	}
	
	
	public static void main(String[] args) throws ClassNotFoundException {
		displayAll();
	}
}
